package org.itfarmer.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.itfarmer.dao.MessageDAO;
import org.itfarmer.dao.pojo.Message;

@SuppressWarnings("unchecked")
public class MessageDAOImpl extends BaseDAOImpl<Message, Integer> implements MessageDAO {

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.dao.MessageDAO#countByReceiver(java.lang.Integer)
	 */
	public Integer countByReceiver(Integer receiver) {
		if (receiver == null) {
			return null;
		}
		this.init();
		String queryString = "from Message where receiver=?";
		Query query = session.createQuery(queryString);
		List rowsList = query.setInteger(0, receiver).list();
		return rowsList == null ? null : rowsList.size();
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.dao.MessageDAO#countBySender(java.lang.Integer)
	 */
	public Integer countBySender(Integer sender) {
		if (sender == null) {
			return null;
		}
		this.init();
		String queryString = "from Message where sender=?";
		Query query = session.createQuery(queryString);
		List rowsList = query.setInteger(0, sender).list();
		return rowsList == null ? null : rowsList.size();
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.dao.MessageDAO#readBySender(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<Message> readBySender(Integer sender, Integer firstRow,
			Integer maxRow) {
		if (sender == null) {
			return null;
		}
		this.init();
		String queryString = "from Message where sender=?";
		Query query = session.createQuery(queryString);
		List<Message> messageList = query.setInteger(0, sender)
																			.setFirstResult(firstRow).setMaxResults(maxRow).list();
		return messageList == null || messageList.size() == 0 ? null : messageList;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.dao.MessageDAO#readByReceiver(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<Message> readByReceiver(Integer receiver, Integer firstRow,
			Integer maxRow) {
		if (receiver == null) {
			return null;
		}
		this.init();
		String queryString = "from Message where receiver=? limit ?, ?";
		Query query = session.createQuery(queryString);
		List<Message> messageList = query.setInteger(0, receiver).setFirstResult(firstRow).setMaxResults(maxRow).list();
		return messageList == null || messageList.size() == 0 ? null : messageList;
	}

}
 